package com.pudu.mydemo.act;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.pudu.mydemo.entity.ActionMessage;
import com.pudu.mydemo.entity.Contant;
import com.pudu.mydemo.universalvideoview.UniversalMediaController;
import com.pudu.mydemo.universalvideoview.UniversalVideoView;
import com.pudu.mydemo.utils.BeepManager;
import com.pudu.mydemo.utils.MyAnimationDrawable;
import com.pudu.mydemo.utils.ToastUtils;
import com.pudu.pdrobot.App;
import com.pudu.pudusclient.CanDataHandle;
import com.pudutech.pdrobot.R;

import java.util.Calendar;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

/**
 * Created by guiliu on 2016/7/15.
 */
public class SmailActivity extends BaseHomeActivity implements UniversalVideoView.VideoViewCallback/*, CanDataHandle.StateChangeCallback*/{
    private static final String TAG = "SmailActivity";
    private ImageButton home;
    private ImageButton begin,pause;
    ImageView imageView, tip_image;
    private int first, then ,after,goback,current;
    private RelativeLayout error_tip;
    private Intent intent;
    private int status;
    private boolean isStart;
    private boolean isEnd;
    private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";
    private String VIDEO_URL;
    UniversalVideoView mVideoView;
    private   TextView  hTextView,video_tip;
    View mVideoLayout;
    UniversalMediaController mMediaController;
    private int mSeekPosition;
    private int cachedHeight;
    private boolean isFullscreen;
    private boolean seekComplete, animatorComplete, normalComplete;
    private BeepManager beepManager;
    private FrameLayout all;
//    private CanDataHandle canDataHandle;

    public String mCustomString;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smail;
    }

    @Override
    public void initView() {
        home = (ImageButton)findViewById(R.id.home);
        begin = (ImageButton)findViewById(R.id.begin);
        pause = (ImageButton)findViewById(R.id.pause);
        imageView = (ImageView)findViewById(R.id.animation1);
        error_tip = (RelativeLayout)findViewById(R.id.tip_error);
        tip_image = (ImageView)findViewById(R.id.tip_image);
        mVideoLayout = findViewById(R.id.video_layout);
        mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
        hTextView = (TextView) findViewById(R.id.text);
        video_tip = (TextView)findViewById(R.id.video_tip);
        all = (FrameLayout)findViewById(R.id.all);
    }

    @Override
    public void initEvent() {
        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmailActivity.this, HomeActivity.class);
                ActionMessage actionMessage = new ActionMessage(Contant.ACTION_PAUSE,"-1");
                ActionMessage actionMessage1 = new ActionMessage(Contant.ACTION_HOME,"-1");
                Gson gson = new Gson();
                String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ACTION_PAUSE", "data":"-1" }
                String JsonMessage2 = gson.toJson(actionMessage1);//Json: { "action": "ACTION_HOME", "data":"-1" }
                Log.e(TAG,"pause json message:"+JsonMessage);
                Log.e(TAG,"home json message:"+JsonMessage2);
                if(App.instance.canbusService.mCanDataHandle != null /*&& !isEnd && isStart*/) {
                    App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                    App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage2);
//                    isStart = false;
//                    isEnd = true;
                }
                startActivity(intent);
                SmailActivity.this.finish();
            }
        });

        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActionMessage actionMessage = new ActionMessage(Contant.ACTION_GO,"-1");
                Gson gson = new Gson();
                String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ACTION_SMART_MOVE", "data":"2" }
                Log.e(TAG,"begin json message:"+JsonMessage);
                if(App.instance.canbusService.mCanDataHandle != null /*&& !isStart && isEnd*/) {
                    App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                    begin.setVisibility(View.GONE);
                    pause.setVisibility(View.VISIBLE);
                    isStart = true;
//                    isEnd = false;
//                    begin.setVisibility(View.INVISIBLE);
//                    pause.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(SmailActivity.this,"服务未启动！示意图、坐标图未上传",Toast.LENGTH_LONG).show();
                }
//                changeAnimationOlder();


                /*Intent intent = new Intent();
                intent.setAction("something_wrong");
                intent.putExtra("status",i++);
                App.context().sendBroadcast(intent);*/
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(getApplicationContext(),"暂停",Toast.LENGTH_LONG).show();

                ActionMessage actionMessage = new ActionMessage(Contant.ACTION_PAUSE,"-1");
                Gson gson = new Gson();
                String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ACTION_SMART_MOVE", "data":"2" }
                Log.e(TAG,"pause json message:"+JsonMessage);
                if(App.instance.canbusService.mCanDataHandle != null /*&& !isEnd && isStart*/) {
                    App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                    begin.setVisibility(View.VISIBLE);
                    pause.setVisibility(View.GONE);
                    isStart = false;
//                    isEnd = true;
//                    begin.setVisibility(View.INVISIBLE);
//                    pause.setVisibility(View.VISIBLE);
                }else {
                    Toast.makeText(SmailActivity.this,"服务未启动！示意图、坐标图未上传",Toast.LENGTH_LONG).show();
                }
            }
        });

        imageView.setOnClickListener(new NoDoubleOnclick(){

            @Override
            public void onNoDoubleClick(View v) {
                pause();
                if(status !=  Contant.SMAIL_NORMAL) {
                    return;
                }
                if(current == first){
                    current = then;
                }else if(current == then) {
                    current = after;
                }else if(current == after) {
                    current = goback;
                }
                if(current != 0) {
                    playAnimation(current, false);
                } else {
                    current = first;
                    playAnimation(current, true);
                }
            }
        });

        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pause();
            }
        });
    }


    public void pause() {
        ActionMessage actionMessage = new ActionMessage(Contant.ACTION_PAUSE,"-1");
        Gson gson = new Gson();
        String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ACTION_SMART_MOVE", "data":"2" }
        Log.e(TAG,"pause json message:"+JsonMessage);
        if(App.instance.canbusService.mCanDataHandle != null /*&& !isEnd && isStart*/) {
            App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
            begin.setVisibility(View.VISIBLE);
            pause.setVisibility(View.GONE);
            isStart = false;
        }else {
            Toast.makeText(SmailActivity.this,"服务未启动！示意图、坐标图未上传",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onStateNotSaved() {
        super.onStateNotSaved();
    }

    @Override
    protected void onResume() {
        super.onResume();
       /* canDataHandle = CanDataHandle.getInstance();
        if(canDataHandle != null)
            canDataHandle.setStateChangeCallback(this,this);*/
        seekComplete = false;
        animatorComplete = false;
        intent = getIntent();
        status = intent.getIntExtra("status",Contant.SMAIL_NORMAL);  //SMAIL_NORMAL默认不携带任何状态，也就是正常进入该界面
        mCustomString = intent.getStringExtra(Contant.CUSTOM_INFORMATION_STRING);
        Log.e("brocast","status:"+status);
        beepManager = new BeepManager(this, status);

        if( status == Contant.CUSTOM_INFORMATION_ID &&
                mCustomString != null &&
                mCustomString.isEmpty() == false ) {
            //显示自定义提示内容
            all.setVisibility(View.VISIBLE);
            int  video = R.raw.error_not_run;
            String tip = mCustomString;
            video = R.raw.hit_cry;
            mVideoLayout.setVisibility(View.VISIBLE);
            VIDEO_URL = "android.resource://"+this.getPackageName()+"/" +video;

            mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);

            mVideoView.setMediaController(mMediaController);
            mMediaController.setVisibility(View.INVISIBLE);
            setVideoAreaSize();
            mVideoView.setVideoViewCallback(this);
            mVideoView.start();
            beepManager.playBeepSoundAndVibrate();
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d(TAG, "onCompletion ");
                    mVideoView.release(true);
                    finish();
                }
            });
            mVideoView.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {

                    if( !seekComplete) {
                        seekComplete = true;
                        //完成了一次
                        ActionMessage actionMessage = new ActionMessage(Contant.ANIMATION_FIRST_TIME_COMPLETE,"-1");
                        Gson gson = new Gson();
                        String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ANIMATION_FIRST_TIME_COMPLETE", "data":"-1" }
                        if(App.instance.canbusService.mCanDataHandle != null ) {
                            App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                        }
                        Log.e(TAG,"video animation complete once");
                    }
                }
            });
            video_tip.setVisibility(View.VISIBLE);

            video_tip.setText(tip);

            return;
        }

        if(!setVideo()) {
            all.setVisibility(View.GONE);
            mVideoLayout.setVisibility(View.GONE);
            MyAnimationDrawable.setShouldRun(true);
            first = R.drawable.smail_animation;
            setOlderByStatus(status);
            current = first;
            playAnimation(first, true);
            animatorGoon();
            animatorTip(status);
        }

       /* if(status >= 5) {
            mVideoLayout.setVisibility(View.VISIBLE);
            setVideo();
        } else {
            mVideoLayout.setVisibility(View.GONE);
            MyAnimationDrawable.setShouldRun(true);
            first = R.drawable.smail_animation;
            setOlderByStatus(status);
            current = first;
            playAnimation(first, true);
            animatorGoon();
            animatorTip(status);
        }*/
        move();
    }

    public boolean  setVideo() {
        all.setVisibility(View.VISIBLE);
        int  video = R.raw.error_not_run;
        String tip = "";
        if(status == Contant.GET_FOOD) {
            //获取食物
            video = R.raw.get_food_smail;
        } else if(status == Contant.HIT_CRY_ERROR) {
            //碰撞错误
            video = R.raw.hit_cry;
        }else if(status == Contant.RUN__NOTKNOW_ERROR) {
            //未知运行错误
            video = R.raw.error_not_run;
            tip = getString(R.string.can_not_run);
        } else if(status == Contant.SINGAL_LOSE_ERROR) {
            //信号丢失错误
            video = R.raw.wifi_lose;
            tip = getString(R.string.uwb_signal_lose);
        } else if(status == Contant.TO_HEAVY_ERROR) {
            //负载过重错误
            video = R.raw.to_heavy;
            tip = getString(R.string.to_heavy_cut);
        } else {
            return false;
        }
        mVideoLayout.setVisibility(View.VISIBLE);
        VIDEO_URL = "android.resource://"+this.getPackageName()+"/" +video;

        mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);

        mVideoView.setMediaController(mMediaController);
        mMediaController.setVisibility(View.INVISIBLE);
        setVideoAreaSize();
        mVideoView.setVideoViewCallback(this);
        mVideoView.start();
        beepManager.playBeepSoundAndVibrate();
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "onCompletion ");
                mVideoView.release(true);
                finish();
            }
        });
        mVideoView.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
//                Log.d(TAG, "onSeekComplete ");  //没完成一次回调一次
                if( !seekComplete) {
                    seekComplete = true;
                    //完成了一次
                    ActionMessage actionMessage = new ActionMessage(Contant.ANIMATION_FIRST_TIME_COMPLETE,"-1");
                    Gson gson = new Gson();
                    String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ANIMATION_FIRST_TIME_COMPLETE", "data":"-1" }
                    if(App.instance.canbusService.mCanDataHandle != null ) {
                        App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                    }
                    Log.e(TAG,"video animation complete once");
                }
            }
        });
        video_tip.setVisibility(View.VISIBLE);
//        video_tip.setAnimateType(HTextViewType.RAINBOW);
//        video_tip.animateText(tip);
        video_tip.setText(tip);
        return true;
    }

    public void animatorTip(int status){
            if(status == Contant.SMAIL_NORMAL) {
                error_tip.setVisibility(View.GONE);
                hTextView.setVisibility(View.GONE);
                return;
            }
        error_tip.setVisibility(View.VISIBLE);
        hTextView.setVisibility(View.VISIBLE);
//        hTextView.setAnimateType(HTextViewType.RAINBOW);
        // animate
//        Shimmer shimmer = new Shimmer();
//        shimmer.start((ShimmerTextView)tip_error);
//            error_tip.setVisibility(View.VISIBLE);
            if(status == Contant.LEFT_REVOLVER_BLOCK_ERROR) {
                hTextView.setText(getString(R.string.can_not_run_left));
//                tip_error.setText("机器人无法运行，请检查左边轮子是否正常工作");
            } else if(status == Contant.RIGHT_REVOLVER_BLOCK_ERROR) {
                hTextView.setText(getString(R.string.can_not_run_right));
//                tip_error.setText("机器人无法运行，请检查右边轮子是否正常工作");
            } else if(status == Contant.LEFT_RIGHT_REVOLVER_BLOCK_ERROR) {
                hTextView.setText(getString(R.string.can_not_run_left_right));
//                tip_error.setText("机器人无法运行，请检查轮子是否正常工作");
            } else if(status == Contant.NO_ELECTRICITY_ERROR) {
                hTextView.setText(getString(R.string.no_elect_tip));
//                tip_error.setText("机器人电量过低");
            }else {
                hTextView.setText("");
            }

    }

    public void animatorGoon() {
        //当前不在该界面，发生错误时弹出动画效果
        if("smail_action_move_error".equals(intent.getAction()) && status != Contant.SMAIL_NORMAL) {
            current = then;
            playAnimation(then, false);
            beepManager.playBeepSoundAndVibrate();
        } else if(status == Contant.SMAIL_NORMAL) {
            normalComplete = true;
        }
    }

    public void move() {
        if("Intent_Action_Move".equals(intent.getAction())) {
            //获取动作action,以及携带信息data(均为字符串)
            String action = intent.getStringExtra("action");
            String data = intent.getStringExtra("data");
            ActionMessage actionMessage = new ActionMessage(action,data);
            Gson gson = new Gson();
            String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ACTION_SMART_MOVE", "data":"2" }
            Log.e(TAG,"move json message:"+JsonMessage);
            //调用运动类代码
            if(App.instance.canbusService.mCanDataHandle != null) {
                App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                isStart = true;
                begin.setVisibility(View.INVISIBLE);
                pause.setVisibility(View.VISIBLE);
            } else {
                ToastUtils.showToast(this,"App服务未启动，请先上传示意图和坐标图", Toast.LENGTH_LONG);
            }

        }
    }


    public void setOlderByStatus(int status) {
        if(status == Contant.SMAIL_NORMAL) {
            then = R.drawable.smail_touch_animation;
            after = 0;
            goback = 0;
        } else if(status == Contant.LEFT_REVOLVER_BLOCK_ERROR) {
            //左轮被挡住
            then = R.drawable.smail_left_animation;
            after = R.drawable.smail_inleft_animation;
            goback = R.drawable.smail_left__back_animation;
        } else if(status == Contant.RIGHT_REVOLVER_BLOCK_ERROR) {
            //右轮被挡住
            //中间-》右边笑-》中间
            then = R.drawable.smail_center__right_animation;
            after = R.drawable.smail_inright_animation;
            goback = R.drawable.smail_right_center_animation;
        } else if(status == Contant.LEFT_RIGHT_REVOLVER_BLOCK_ERROR) {
            //左右轮同时被挡住
            //中间-》左边眨眼-》右边眨眼-》中间
            Log.e("guiliu","changeAnimationOlder");
            then = R.drawable.smail_left_animation;
            after = R.drawable.smail_left_right_back_animation;
            goback = R.drawable.smail_right_center_animation;
        } else if(status == Contant.NO_ELECTRICITY_ERROR) {
            //电量低
            then = R.drawable.no_electricity_start_animation;
            after = R.drawable.no_electricity_go_animation;
            goback = R.drawable.no_electricity_back_animation;
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    public void onStart(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onStart UniversalVideoView callback");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyAnimationDrawable.stop();
        if (mVideoView != null && mVideoView.isPlaying()) {
            mSeekPosition = mVideoView.getCurrentPosition();
            Log.d(TAG, "onPause mSeekPosition=" + mSeekPosition);
            mVideoView.pause();
            mVideoView.release(true);
        }
        beepManager.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyAnimationDrawable.stop();
        if (mVideoView != null && mVideoView.isPlaying()) {
            mSeekPosition = mVideoView.getCurrentPosition();
            Log.d(TAG, "onDestroy mSeekPosition=" + mSeekPosition);
            mVideoView.pause();
            mVideoView.release(true);
        }
       /* if(canDataHandle != null) {
            canDataHandle.detachedSmail();
        }*/
    }

    public void playAnimation(int resId, final boolean oneShot) {
        if(resId == 0) return;
        MyAnimationDrawable.setOneShot(oneShot);
        MyAnimationDrawable.animateRawManuallyFromXML(resId, imageView, new Runnable() {
            @Override
            public void run() {
                // TODO onStart
                // 动画开始时回调
            }
        }, new Runnable() {
            @Override
            public void run() {
                // TODO onComplete
                // 动画结束时回调
//                Log.e("guiliu","onComplete oneShot"+ oneShot);
                if(animatorComplete || normalComplete) {
                    animatorComplete = false;
                    normalComplete = false;
                    ActionMessage actionMessage = new ActionMessage(Contant.ANIMATION_FIRST_TIME_COMPLETE,"-1");
                    Gson gson = new Gson();
                    String JsonMessage = gson.toJson(actionMessage); //Json: { "action": "ANIMATION_FIRST_TIME_COMPLETE", "data":"-1" }
                    if(App.instance.canbusService.mCanDataHandle != null ) {
                        App.instance.canbusService.mCanDataHandle.getControlFromUI(JsonMessage);
                    }
                    Log.e(TAG,"animation complete once");
                }
                if(!oneShot)  {
                    boolean again = false;
                    if(current == R.drawable.smail_touch_animation) {
//                        Log.e("guiliu","onComplete oneShot"+ current);
                        current = first;
                        playAnimation(current, true);
                        return;
                    }
                    if(current == first){
                        current = then;
                        again = false;
                    }else if(current == then) {
                        current = after;
                        again = true;
                        animatorComplete = true;
                    }else if(current == after) {
                        current = goback;
                        again = false;
                    }else if(current == goback) {
                        current = first;
                        again = true;
                    }

                    if(current !=0) {
                        playAnimation(current, again);
                    } else {
                        current = first;
                        playAnimation(current, true);
                    }

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    /*@Override
    public void onStateChange(int status) {
        Log.e("Smail","onStateChange status:"+status);
        if(status == Contant.PAUSE) {
            //修改開始、暫停按鈕為開始模式
            begin.setVisibility(View.VISIBLE);
            pause.setVisibility(View.GONE);
        }
    }*/

    public abstract class NoDoubleOnclick implements View.OnClickListener{
        public static final int MIN_CLICK_DELAY_TIME = 1000;
        private long lastClickTime = 0;
        @Override
        public void onClick(View v) {
            long currentTime = Calendar.getInstance().getTimeInMillis();
            if(currentTime - lastClickTime > MIN_CLICK_DELAY_TIME ){
                lastClickTime = currentTime;
                onNoDoubleClick(v);
            }
        }

        public abstract void onNoDoubleClick(View v);
    }

    /**
     * 置视频区域大小
     */
    private void setVideoAreaSize() {
        mVideoLayout.post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
                mVideoLayout.setLayoutParams(layoutParams);


//                int width = mVideoLayout.getWidth();
//                cachedHeight = (int) (width * 405f / 720f);
////                cachedHeight = (int) (width * 3f / 4f);
////                cachedHeight = (int) (width * 9f / 16f);
//                ViewGroup.LayoutParams videoLayoutParams = mVideoLayout.getLayoutParams();
//                videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                videoLayoutParams.height = 600;
//                mVideoLayout.setLayoutParams(videoLayoutParams);
                mVideoView.setVideoPath(VIDEO_URL);
                mVideoView.requestFocus();
            }
        });
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState Position=" + mVideoView.getCurrentPosition());
        outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
        Log.d(TAG, "onRestoreInstanceState Position=" + mSeekPosition);
    }


    @Override
    public void onScaleChange(boolean isFullscreen) {
        this.isFullscreen = isFullscreen;
        if (isFullscreen) {
            ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            mVideoLayout.setLayoutParams(layoutParams);

        } else {
            ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = this.cachedHeight;
            mVideoLayout.setLayoutParams(layoutParams);
        }

        switchTitleBar(!isFullscreen);
    }

    private void switchTitleBar(boolean show) {
        android.support.v7.app.ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            if (show) {
                supportActionBar.show();
            } else {
                supportActionBar.hide();
            }
        }
    }

    @Override
    public void onBufferingStart(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onBufferingStart UniversalVideoView callback");
    }

    @Override
    public void onBufferingEnd(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onBufferingEnd UniversalVideoView callback");
    }

    @Override
    public void onBackPressed() {
        if (this.isFullscreen) {
            mVideoView.setFullscreen(false);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onPause(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onPause UniversalVideoView callback");
    }



}
